package com.zc.microservice.system.core.api.service;

import com.zc.auth.security.core.access.meta.UrlPattenRoleEntity;
import com.zc.auth.security.core.access.meta.UrlRoleMetaDataManager;
import com.zc.microservice.system.core.infra.service.SystemUrlRoleService;
import com.zc.microservice.user.api.UserApi;
import com.zc.microservice.user.common.result.UserDetailResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * url和角色
 *
 * @author zcj
 * @version 1.0.0
 * @date 2022/1/21 10:24
 */
@Service
public class PattenAndRoleService {

    @Autowired
    private SystemUrlRoleService systemUrlRoleService;
    @Autowired
    private UrlRoleMetaDataManager urlRoleMetaDataManager;
    @DubboReference(check = false)
    private UserApi userApi;

    /**
     * 更新角色url权限对照表
     *
     * @param serverName
     */
    public void updateRolePatten(String serverName) {
        List<UrlPattenRoleEntity> list = systemUrlRoleService.queryRoleUrlPattens(serverName);
        Map<String, Set<String>> map = list.stream().collect(
                Collectors.groupingBy(UrlPattenRoleEntity::getUrlPatten,
                        Collectors.mapping(UrlPattenRoleEntity::getRoleCode, Collectors.toSet())));
        urlRoleMetaDataManager.updateRedisUrlsAndRolesMap(map, serverName);
    }

    public void threadLocalTest() {

        UserDetailResult detailResult = userApi.queryUserDetail("111");
        System.out.println(detailResult);

    }


}
